﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Microsoft.SharePoint;

namespace AWS.SLAM.Hierarchy
{
    public static class SPListItemExtender
    {
        public static string GetHierarchyPositionFieldValue(this SPListItem listItem)
        {
            string hierarchyPositionFieldValue = "";
            foreach (SPField field in listItem.Fields)
            {
                if (field is HierarchyPositionField && listItem[field.Id] != null)
                {
                    hierarchyPositionFieldValue = listItem[field.Id].ToString();
                    break;
                }
            }

            return hierarchyPositionFieldValue;
        }

        public static string HoldHierarchyPositionFieldValue(this SPListItem listItem)
        {
            string hierarchyPositionFieldValue = "";
            foreach (SPField field in listItem.Fields)
            {
                if (field is HierarchyPositionField && listItem[field.Id] != null)
                {
                    hierarchyPositionFieldValue = listItem[field.Id].ToString();
                    if (!String.IsNullOrEmpty(hierarchyPositionFieldValue))
                    {
                        listItem[field.Id] = hierarchyPositionFieldValue.TrimEnd('a', 'm', 's') + "s";
                        listItem.SystemUpdate(false);
                    }
                    break;
                }
            }

            return hierarchyPositionFieldValue;
        }
    }
}
